package server;

import container.MyContext;
import container.MyHost;
import container.MyWrapper;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

public class MyRequestProcessor  extends Thread{

//    private Socket socket;
//
//    private Map<String,MyHttpServlet> urlToMyHttpServletMap;
//
//    public MyRequestProcessor(Socket socket,Map<String,MyHttpServlet> urlToMyHttpServletMap){
//        this.socket = socket;
//        this.urlToMyHttpServletMap = urlToMyHttpServletMap;
//    }
//
//
//    @Override
//    public void run() {
//        //接收请求
//        //获取输入流
//        try {
//            InputStream inputStream = socket.getInputStream();
//            //获取输出流
//            OutputStream outputStream = socket.getOutputStream();
//            Response response = new Response(outputStream);
//            //获取url
//            Request request = new Request(inputStream);
//            String url = request.getUrl();
//            //获取对应MyHttpServlet
//            MyHttpServlet myHttpServlet = urlToMyHttpServletMap.get(url);
//            if(myHttpServlet == null){
//                //访问静态资源
//                response.outputHtml(url);
//            }else {
//                //调用MyHttpServlet
//                myHttpServlet.service(request,response);
//            }
//            socket.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

    private Socket socket;
    private String webappsPath;

    private Map<String, MyHost> hostNameToHostMap = new HashMap<>();

    public MyRequestProcessor(Socket socket,Map<String, MyHost> hostNameToHostMap, String webappsPath) {

        this.socket = socket;
        this.hostNameToHostMap = hostNameToHostMap;
        this.webappsPath = webappsPath;
    }

//    @Override
//    public void run() {
//        try{
//
//            InputStream inputStream = socket.getInputStream();
//            // 封装Request对象和Response对象
//            Request request = new Request(inputStream);
//            Response response = new Response(socket.getOutputStream());
//
//            Map<String,Map<String,MyHttpServlet>> contexts = hosts.get(request.getHost());//得到虚拟机的项目map
//
//
//            if(contexts == null) {
//                System.out.println("未找到虚拟机");
//                response.outputHtml(webappsPath+"404");
//                return;
//            }
//            String url = request.getUrl();
//            String webName = url.split("/")[1];
//            Map<String,MyHttpServlet> wrappers = contexts.get(webName);//得到对应项目的wrapper集
//            if(wrappers == null) {
//                System.out.println("未找到对应项目");
//                response.outputHtml(webappsPath+"404");
//                return;
//            }
//            String replace = url.replace("/" + url.split("/")[1], "");
//            MyHttpServlet httpServlet = wrappers.get(replace);
//
//
//            // 静态资源处理
//            if(httpServlet == null) {
//                System.out.println("静态资源处理");
//                response.outputHtml(webappsPath+request.getUrl());
//            }else{
//                // 动态资源servlet请求
//                System.out.println("动态资源servlet请求");
//                httpServlet.service(request,response);
//            }
//            socket.close();
//        }catch (Exception e) {
//            e.printStackTrace();
//        }
//
//    }

    @Override
    public void run() {
        try{

            InputStream inputStream = socket.getInputStream();
            // 封装Request对象和Response对象
            Request request = new Request(inputStream);
            Response response = new Response(socket.getOutputStream());

            MyHost myHost = hostNameToHostMap.get(request.getHost());//得到虚拟机的项目map
            MyContext[] contexts = myHost.getContexts();


            if(contexts.length == 0) {
                System.out.println("未找到虚拟机");
                response.outputHtml(myHost.getAppBase()+"404");
                return;
            }
            String url = request.getUrl();
            String[] split = url.split("/");
            String webName = split[1];
            String urlPattern = "/" + webName;
            if(split.length > 2){
                urlPattern = url.split("/")[2];
                urlPattern = "/" + urlPattern;
            }
            for (MyContext context : contexts) {
                if(webName.equals(context.getContextName())){
                    Map<String, MyWrapper> myWrapperMap = context.getMyWrapperMap();
                    MyWrapper myWrapper = myWrapperMap.get(urlPattern);
                    if(myWrapper == null){
                        System.out.println("静态资源处理");
                        response.outputHtml(myHost.getAppBase()+request.getUrl());
                        break;
                    }else {
                        // 动态资源servlet请求
                        MyServlet servlet = myWrapper.getServlet();
                        System.out.println("动态资源servlet请求");
                        servlet.service(request,response);
                        break;
                    }

                }
            }

//            Map<String,MyHttpServlet> wrappers = contexts.get(webName);//得到对应项目的wrapper集
//            if(wrappers == null) {
//                System.out.println("未找到对应项目");
//                response.outputHtml(webappsPath+"404");
//                return;
//            }
//            String replace = url.replace("/" + url.split("/")[1], "");
//            MyHttpServlet httpServlet = wrappers.get(replace);
//
//
//            // 静态资源处理
//            if(httpServlet == null) {
//                System.out.println("静态资源处理");
//                response.outputHtml(webappsPath+request.getUrl());
//            }else{
//                // 动态资源servlet请求
//                System.out.println("动态资源servlet请求");
//                httpServlet.service(request,response);
//            }
            socket.close();
        }catch (Exception e) {
            e.printStackTrace();
        }

    }
}
